import {
	dateToMonthDay,
	nowSecond
} from "../utils.js"
/**
 * 存储被占用的区块位置信息
 */
class TimeNumberPosition {

	_hourHeightInPx; // 一小时占用的像素高度
	_parentHeight; // 父组件高度
	_zeroPosition; // 初始化时候的位置，一般为0
	_zeroTime; // 初始化时对应的时间
	_currentPosition; // 当前滑动到的位置（相对于_zeroPosition）
	_currentTime; // currentPosition对应的时间
	_layer = null; // 父组件


	// 可用的konva组件
	_freeKonva = [
		// {
		// 	use: false, 	// 是否正在使用
		// 	text: null, 	// 文本组件
		// 	line: null, 	// 线条组件
		// 	position: 0, 	// 位置信息
		// }
	]

	/**
	 * 初始化函数
	 */
	init = (position, layer, hourHeightInPx, parentHeight) => {
		this._parentHeight = parentHeight;
		this._hourHeightInPx = hourHeightInPx;
		this._layer = layer;
		this._zeroPosition = position;
		this._currentPosition = position;
		this._zeroTime = nowSecond();

		const now = this._zeroTime;


		// 向后寻找可以显示的小时线
		const date = new Date();
		const hourStr = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() +
				":00:00";
		let hourTime = (new Date(hourStr) / 1000).toFixed();
		let hourPosition = parseFloat(now - this._zeroTime) / 60.0 / 60.0;
		
		if(hourPosition < 0){
			hourPosition += this._hourHeightInPx;
		}
		let line = {};
		line.position = hourPosition;
		line.text = date.getHours();
		line.date = new Date(date);
		line.show = line.position <= this._parentHeight && line.position >= 0;
		while(line.show){
			const konva = this._getOrCreateKonva(line.position);
			konva.text.text(line.text);
			konva.text.y(line.position);
			konva.line.y(line.position);
			konva.position = line.position;
			konva.use = true;
			
			hourPosition += this._hourHeightInPx;
			date.setHours(date.getHours() + 1)
			
			line = {};
			line.position = hourPosition;
			line.text = date.getHours();
			line.date = new Date(date);
			line.show = line.position <= this._parentHeight && line.position >= 0;
		}
		
	}


	/**
	 * 滑动
	 */
	move = (distance) => {
		this._currentPosition -= distance.deltaY;
		let currentTime = parseInt(this._zeroTime);
		const hour = (parseFloat(this._currentPosition) 
				/ parseFloat(this._hourHeightInPx)).toFixed(2);
				
		// console.log("hour", hour)
		// console.log("this._currentPosition", this._currentPosition)

		currentTime += parseInt((hour * 60 * 60).toFixed());
		// currentTime *= 1000;


		this._unuseAllKonva()

		// 向后寻找可以显示的日期线
		const date = new Date(currentTime * 1000);
		// console.log("currentTime" , date)
		const hourStr = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() +
				":00:00";
		let hourTime = (new Date(hourStr).getTime() / 1000).toFixed();
		let hourPosition = parseInt(((parseFloat(hourTime - currentTime) / 60.0 / 60.0) * this._hourHeightInPx).toFixed());
		// console.log("hourPosition", hourPosition);
		
		if(hourPosition < 0){
			hourPosition += this._hourHeightInPx;
		}
		let line = {};
		line.position = hourPosition;
		line.text = date.getHours();
		line.date = new Date(date);
		line.show = line.position <= this._parentHeight && line.position >= 0;
		// console.log("time number position line ", line);
		while(line.show){
			const konva = this._getOrCreateKonva(line.position);
			konva.text.text(line.text);
			konva.text.y(line.position);
			konva.line.y(line.position);
			konva.line.show();
			konva.text.show();
			konva.position = line.position;
			konva.use = true;
			
			hourPosition += this._hourHeightInPx;
			date.setHours(date.getHours() + 1)
			
			line = {};
			line.position = hourPosition;
			line.text = date.getHours();
			line.date = new Date(date);
			line.show = line.position <= this._parentHeight && line.position >= 0;
		}

		// console.log("konva.size ", this._freeKonva.length);

		this._hideUnuseKonva();
	}


	/**
	 * 私有方法，复位所有创建组件的状态
	 */
	_unuseAllKonva = () => {
		for (let i in this._freeKonva) {
			const konva = this._freeKonva[i];
			konva.use = false;
		}
	}


	/**
	 * 隐藏所有不适用的组件
	 */
	_hideUnuseKonva = () => {
		for (let i in this._freeKonva) {
			const konva = this._freeKonva[i];
			if (!konva.use) {
				konva.line.hide();
				konva.text.hide();
			}
		}
	}


	/**
	 * 获取或创建konva组件
	 */
	_getOrCreateKonva = () => {
		for (let i in this._freeKonva) {
			const konva = this._freeKonva[i];
			if (!konva.use) {
				konva.use = true;
				return konva;
			}
		}

		const konva = {};
		konva.text = new Konva.Text({
			x: 100,
			y: 0,
			text: "",
			fontSize: 15,
			fontFamily: 'Calibri',
			fill: 'green'
		});
		this._layer.add(konva.text);
		konva.line = new Konva.Line({
			points: [0, 0, 0, 0],
			stroke: 'green',
			strokeWidth: 2,
			lineJoin: 'round',
			dash: [33, 10],
		})
		this._layer.add(konva.line);
		konva.position = 0;
		konva.use = true;
		this._freeKonva.push(konva);
		return konva;
	}
}

export default TimeNumberPosition;
